package com.ll.arraydemo;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @author 刘龙
 * @version 1.0
 */
public class ArrayMethod01 {
    public static void main(String[] args) {

        Integer[] integers = {1, 3, 9, 2, 5,};

        // 直接打印输出地址
        // [Ljava.lang.Integer;@58ceff1
        System.out.println(integers);

        // 遍历数组1
        // 13925
        for (Integer integer : integers) {
            System.out.print(integer);
        }

        // 遍历数组2
        /**
         * public static String toString(Object[] a) {
         *         if (a == null)
         *             return "null";
         *
         *         int iMax = a.length - 1;
         *         if (iMax == -1)
         *             return "[]";
         *
         *         StringBuilder b = new StringBuilder();
         *         b.append('[');
         *         for (int i = 0; ; i++) {
         *             b.append(String.valueOf(a[i]));
         *             if (i == iMax)
         *                 return b.append(']').toString();
         *             b.append(", ");
         *         }
         *     }
         */
        //[1, 3, 9, 2, 5]
        System.out.println(Arrays.toString(integers));

        System.out.println("======================");

        // 排序 sort
        Integer[] arr = {1, -1, 0, 7, 0, 5, 6};
        Arrays.sort(arr);

        // [-1, 0, 0, 1, 5, 6, 7]
        System.out.println(Arrays.toString(arr));

        System.out.println("=====================");

        // 定制排序
        Integer[] arr2 = {1, -1, 0, 7, 0, 5, 6};
        Arrays.sort(arr2, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                Integer i1 = (Integer) o1;
                Integer i2 = (Integer) o2;
                return i2 - i1;
            }
        });

        // [7, 6, 5, 1, 0, 0, -1]
        System.out.println(Arrays.toString(arr2));


    }

    public static void llBubbleSort(Object[] arr) {
        for (int i = 0; i < arr.length; i++) {
            for (int y = 0; y < arr.length - 1 - i; y++) {
                if ((int) (arr[y]) > (int) (arr[y + 1])) {
                    Object temp = arr[y];
                    arr[y] = arr[y + 1];
                    arr[y + 1] = temp;
                }
            }
        }
    }
}
